﻿@page "/"
@inherits IndexPage
@inject INotificationService _notice

<PageTitle>@AppConfig.Branch - @Language["App.SubTitle"]</PageTitle>

<KError OnError="OnError">
    <CascadingValue Value="this">
        @if (IsLoaded)
        {
            @if (!Context.Install.IsInstalled)
            {
                <InstallPage OnInstall="OnInstall">
                    <TopMenu>
                        <ul class="kui-menu">
                            <li style="text-align:center;"><AntLanguage OnChanged="StateChanged" /></li>
                            <li style="margin-left:15px;"><AntTheme Theme="@Theme" OnChanged="SetTheme" /></li>
                        </ul>
                    </TopMenu>
                </InstallPage>
            }
            else if (!IsLogin)
            {
                <Known.Shared.Login OnLogin="OnLogin" />
            }
            else if (UIType == UIType.AntDesign)
            {
                <Known.AntBlazor.Admin OnLogout="OnLogout" />
            }
            else if (UIType == UIType.Bootstrap)
            {
                <Known.BootBlazor.Admin OnLogout="OnLogout" />
            }
        }
    </CascadingValue>
</KError>

@code {
    private const string KeyUIType = "Known_UIType";
    [Inject] private Known.AntBlazor.UIService AntUI { get; set; }
    [Inject] private Known.BootBlazor.UIService BootUI { get; set; }

    protected override async Task OnInitAsync()
    {
        UIType = await JS.GetLocalStorageAsync<UIType>(KeyUIType);
        SetUIService();
        await base.OnInitAsync();
    }

    public UIType UIType { get; private set; }

    public async void SetUIType(UIType type)
    {
        UIType = type;
        SetUIService();
        await JS.SetLocalStorageAsync(KeyUIType, type);
    }

    private void SetUIService()
    {
        Context.UI = AntUI;
        if (UIType == UIType.AntDesign)
            Context.UI = AntUI;
        else if (UIType == UIType.Bootstrap)
            Context.UI = BootUI;
    }

    private Task OnError(Exception ex)
    {
        var config = new NotificationConfig
        {
            Message = Language["Title.Error"],
            Description = ex.Message,
            Placement = NotificationPlacement.BottomRight,
            Duration = null
        };
        if (Config.App.IsDevelopment)
            config.Description = ex.ToString();
        return _notice.Error(config);
    }

    // //第三方登录设置当前用户
    // protected override async Task OnAfterRenderAsync(bool firstRender)
    // {
    //    if (firstRender)
    //        await SetCurrentUserAsync(CurrentUser);
    //    await base.OnAfterRenderAsync(firstRender);
    // }

    // //第三方登录获取当前用户
    // protected override async Task<UserInfo> GetThirdUserAsync()
    // {
    //    //var third = ThirdApi.GetUser();
    //    var third = new { UserName = "admin" };
    //    var user = await Platform.GetUserAsync(third.UserName);
    //    return user;
    // }
}